package controller

import (
	"github.com/PhlexTheCharr/gofurry-cloud-service/apps/openlist/manage/util/models"
	"github.com/PhlexTheCharr/gofurry-cloud-service/apps/openlist/manage/util/service"
	"github.com/PhlexTheCharr/gofurry-cloud-service/common"
	"github.com/gofiber/fiber/v2"
)

/*
 * @Desc: Util 管理
 * @author: 慵狼小洛
 * @version: v1.0.0
 */

type utilManageApi struct{}

var UtilManageApi *utilManageApi

func init() {
	UtilManageApi = &utilManageApi{}
}

// @Summary 新增金句
// @Schemes
// @Description 新增金句
// @Tags Openlist-manage-util
// @Accept json
// @Produce json
// @Param Authorization header string false "用户令牌"
// @Param body body models.AddSayingRequest true "请求body"
// @Success 200 {object} common.ResultData
// @Router /api/ol/util/manage/saying/add [Post]
func (api *utilManageApi) AddSaying(c *fiber.Ctx) error {
	var req models.AddSayingRequest
	if err := c.BodyParser(&req); err != nil {
		return common.NewResponse(c).Error("参数错误: " + err.Error())
	}

	if err := service.GetUtilManageService().AddSaying(req); err != nil {
		return common.NewResponse(c).Error(err.GetMsg())
	}

	return common.NewResponse(c).Success()
}

// @Summary 修改金句
// @Schemes
// @Description 修改金句
// @Tags Openlist-manage-util
// @Accept json
// @Produce json
// @Param Authorization header string false "用户令牌"
// @Param body body models.UpdateSayingRequest true "请求body"
// @Success 200 {object} common.ResultData
// @Router /api/ol/util/manage/saying/update [Post]
func (api *utilManageApi) UpdateSaying(c *fiber.Ctx) error {
	var req models.UpdateSayingRequest
	if err := c.BodyParser(&req); err != nil {
		return common.NewResponse(c).Error("参数错误: " + err.Error())
	}

	if err := service.GetUtilManageService().UpdateSaying(req); err != nil {
		return common.NewResponse(c).Error(err.GetMsg())
	}

	return common.NewResponse(c).Success()
}

// @Summary 分页查询金句
// @Schemes
// @Description 分页查询金句
// @Tags Openlist-manage-util
// @Accept json
// @Produce json
// @Param Authorization header string false "用户令牌"
// @Param body body models.SayingPageQuery true "请求body"
// @Success 200 {object} common.ResultData
// @Router /api/ol/util/manage/saying/page [Post]
func (api *utilManageApi) SayingPage(c *fiber.Ctx) error {
	var req models.SayingPageQuery
	if err := c.BodyParser(&req); err != nil {
		return common.NewResponse(c).Error("参数错误: " + err.Error())
	}

	req.InitPageIfAbsent()
	res, pageErr := service.GetUtilManageService().Page(&req)
	if pageErr != nil {
		return common.NewResponse(c).Error(pageErr.GetMsg())
	}

	return common.NewResponse(c).SuccessWithData(res)
}

// @Summary 删除金句
// @Schemes
// @Description 删除金句
// @Tags Openlist-manage-util
// @Accept json
// @Produce json
// @Param Authorization header string false "用户令牌"
// @Param body body models.SayingDeleteRequest true "请求body"
// @Success 200 {object} common.ResultData
// @Router /api/ol/util/manage/saying/delete [Post]
func (api *utilManageApi) DeleteSaying(c *fiber.Ctx) error {
	var req models.SayingDeleteRequest
	if err := c.BodyParser(&req); err != nil {
		return common.NewResponse(c).Error("参数错误: " + err.Error())
	}

	if err := service.GetUtilManageService().SayingDeleteList(req); err != nil {
		return common.NewResponse(c).Error(err.GetMsg())
	} else {
		return common.NewResponse(c).Success()
	}
}
